package com.cryrabbit.yyystitp.ui.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cryrabbit.yyystitp.R;
import com.cryrabbit.yyystitp.ui.iviews.Iview;
import com.cryrabbit.yyystitp.ui.presenters.LoginPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by CryRabbit on 2016/8/30.
 */

public class LoginActivity extends AppCompatActivity implements Iview{
    @BindView(R.id.activity_login_user)
    EditText userView;
    @BindView(R.id.activity_login_password)
    EditText passwordView;
    @BindView(R.id.activity_login_button1)
    Button button1;
    @BindView(R.id.activity_login_button2)
    Button button2;
    LoginPresenter loginPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        loginPresenter=new LoginPresenter(this);
    }
    @OnClick(R.id.activity_login_button1)
    void register(){
        startActivity(new Intent(this, RegisterActivity.class));
    }
    @OnClick(R.id.activity_login_button2)
    void login(){
        userView.setError(null);
        passwordView.setError(null);
        String user=userView.getText().toString();
        String pass=passwordView.getText().toString();
        boolean cancel=false;
        View focusView=null;
        if(!TextUtils.isEmpty(pass)&&pass.length()<=4){
            passwordView.setError("密码太短");
            focusView=passwordView;
            cancel=true;
        }
        if(TextUtils.isEmpty(user)){
            userView.setError("用户名不能为空");
            focusView=userView;
            cancel=true;
        }
        if(cancel){
            focusView.requestFocus();
        }else{
            loginPresenter.login(user,pass);
        }
        startActivity(new Intent(this,MainActivity.class));
        finish();
    }

    @Override
    public void getResult(String s) {
        Toast.makeText(LoginActivity.this,s,Toast.LENGTH_LONG).show();
    }
}
